//
//  UIImage+Tint.m
//  ImageBlend
//
//  Created by 王 巍 on 13-4-29.
//  Copyright (c) 2013年 OneV-s-Den. All rights reserved.
//

#import "UIImage+Tint.h"

@implementation UIImage (Tint)


- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor
{
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];
}
- (UIImage *) imageWithTintColor:(UIColor *)tintColor alpha:(CGFloat)alpha{
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn alpha:alpha];

}
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor alpha:(CGFloat)alpha{
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay alpha:alpha];
}
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode
{
    return [self imageWithTintColor:tintColor blendMode:blendMode alpha:1.0f];
}
- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha{
    //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    [tintColor setFill];
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    UIRectFill(bounds);
    
    //Draw the tinted image in context
    [self drawInRect:bounds blendMode:blendMode alpha:alpha];
    
    if (blendMode != kCGBlendModeDestinationIn) {
        [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:alpha];
    }
    
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return tintedImage;
}

+ (UIImage *)getImageWithFrame:(CGRect)frame corner:(CGFloat)corner{
    
    //创建bitmap
    UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, NO, 1.0);
    //获取当前画板
    CGContextRef con = UIGraphicsGetCurrentContext();
    //设置背景色
    CGContextSetFillColorWithColor(con, [UIColor colorWithWhite:0.0f alpha:0.7f].CGColor);
    //设置背景色大小
    CGContextFillRect(con, [UIScreen mainScreen].bounds);
    //设置圆角矩形
    float x =frame.origin.x;
    float y = frame.origin.y;
    float h = frame.size.height;
    float w = frame.size.width;
    CGContextMoveToPoint(con, x, y+h-corner/2);
    CGContextAddArcToPoint(con, x, y, x+corner, y, corner/2);
    CGContextAddArcToPoint(con, x+w, y, x+w, y+corner, corner/2);
    CGContextAddArcToPoint(con, x+w , y+h, x+w-corner, y+h, corner/2);
    CGContextAddArcToPoint(con, x, y+h, x, y+h-corner, corner/2);
    
    //
    CGContextSetBlendMode(con, kCGBlendModeClear);
    //封闭
    CGContextClosePath(con);
    //绘图
    CGContextDrawPath(con, kCGPathFillStroke);
    //根据当前bitmap创建image
    UIImage *ima = UIGraphicsGetImageFromCurrentImageContext();
    //移除bitmap
    UIGraphicsEndImageContext();
    
    return ima;
}
@end
